import base64
import io

from PIL import Image, ImageDraw
from app.lib.baidu import baidu
from app.utils.index import base2file


def image_to_base64(file_path):
  with open(file_path, "rb") as image_file:
    encoded_string = base64.b64encode(image_file.read())
  return encoded_string.decode("utf-8")

# source = 'C:\\Users\\liyu\\Pictures\\XXJC2404098462729110065911.jpg'
imgFile = r"E:\新建文件夹\2oONgNDPcB36KGtVWKfmkgXL2ow.jpg"

# 百度抠图取点
lnd = baidu.get_photo_standard_param(image_to_base64(imgFile))
# print(lnd.options)
base2file(r"E:\test.png", lnd.image_base64)

# 标点显示
# print("base64\n", lnd.image_base64)

# with io.open("../../utils/img_base64.txt", "w", encoding="utf-8") as file:
#   file.write(lnd.image_base64)

img_bin = base64.b64decode(lnd.image_base64)
image = Image.open(io.BytesIO(img_bin))
draw = ImageDraw.Draw(image)
point_color = (255, 0, 0)

draw.ellipse([lnd.options.chin_position.x-5, lnd.options.chin_position.y-5, lnd.options.chin_position.x+5, lnd.options.chin_position.y+5], fill=point_color, outline=None)
draw.ellipse([lnd.options.cheek_left.x-5, lnd.options.cheek_left.y-5, lnd.options.cheek_left.x+5, lnd.options.cheek_left.y+5], fill=point_color, outline=None)
draw.ellipse([lnd.options.cheek_right.x-5, lnd.options.cheek_right.y-5, lnd.options.cheek_right.x+5, lnd.options.cheek_right.y+5], fill=point_color, outline=None)
draw.ellipse([lnd.options.nose_tip.x-5, lnd.options.nose_tip.y-5, lnd.options.nose_tip.x+5, lnd.options.nose_tip.y+5], fill=point_color, outline=None)

image.show()
